package fr.esiag.isidis.pdsTeam.views;


import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.UnknownHostException;

import javax.swing.*;

import fr.esiag.isidis.pdsTeam.Enums.Source;
import fr.esiag.isidis.pdsTeam.TramCreator.Tram;
import fr.esiag.isidis.pdsTeam.UDP.RawInformationSender;
import fr.esiag.isidis.pdsTeam.beans.EtatTrain;
import fr.esiag.isidis.pdsTeam.beans.Ligne;
import fr.esiag.isidis.pdsTeam.beans.Train;
public class TrainView extends JPanel implements ActionListener{
	public EtatTrain getEtat(){
		return etat;
	}
	boolean inStation;
	boolean enMarche=false;
	boolean terminus;
	Image backGround;
	Integer x,y;
	int dx;
	Boolean right;
	int count;
	Ligne ligne;
	LigneView ligneView;
	int sleepTemps;
	Train train;
	EtatTrain etat;
	private int station;
	Timer timer;
	public TrainView(int dx,boolean right,Ligne ligne,LigneView ligneView,Train train){
		this.train=train;
		
		timer=new Timer(2000,this);
		//timer.start();
		//demarrer();
		this.ligneView=ligneView;
		this.ligne=ligne;
		if(right)
		{
			y=ligneView.getPosY()-17;
			backGround=ResourceImage.getInstance().getTrainR();
			x=ligneView.getPosX()-48;
			station=0;
		}
		else
		{
			y=ligneView.getPosY()+38;
			backGround=ResourceImage.getInstance().getTrainL();
			x=ligneView.getPosX()-3+(ligne.getNbStation()-1)*ligneView.getDistance();
			station=ligne.getNbStation()-1;
		}
		this.dx=dx;
		this.right=right;
		etat=new EtatTrain();
	}
	int isStation(){
		if(!right)
		{
			if(x==ligneView.getPosX()) {
				terminus=true;
				return 0;
			}
		}
		else
		{
			if(x==ligneView.getPosX()-19-24+(ligne.getNbStation()-1)*ligneView.getDistance()){
				terminus=true;
				return ligne.getNbStation()-1;
			}
		}
		for(int i=0;i<ligne.getNbStation();i++)
		{
			if(!right)
			{
				
				if(x==ligneView.getPosX()+(i)*ligneView.getDistance()){
					return i;
				}
			}
			else
			{
				if(x==ligneView.getPosX()-19-24+(i+1)*ligneView.getDistance()){
					return (i+1);
				}
			}
		}
		return -1;
	}
	public void move() throws UnknownHostException
	{
		if(!enMarche)
		{
			return;
		}
		if(sleepTemps>0){
			sleepTemps--;
				return;
		}
		int s = isStation();
		if(s!=-1){
			station=s;
			if(terminus) 
				{
					byte[] position=Tram.getInstance().setTram(Source.Train.ordinal(),train.getId(),station);
					RawInformationSender.getInstance().sendTram(position);
					arreter();
					//System.out.println("Terminus");
				}
			else
			{
				byte[] stationA=Tram.getInstance().setTram(Source.Train.ordinal(),train.getId(),station);
				RawInformationSender.getInstance().sendTram(stationA);
				//System.out.println("Station :");
				
			}
			sleepTemps=100;
		}
		if(right)
		x=x+dx;
		else
		x=x-dx;
	}
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	public Image getImage(){
		return backGround;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public void demarrer(){
		enMarche=true;
		timer.start();
	}
	public void arreter(){
		enMarche=false;
		timer.stop();
		etat.change(x, y, right,station,train.getId(),terminus);
	}
	public boolean isEnMarche() {
		return enMarche;
	}
	public void actionPerformed(ActionEvent e) {
		etat.change(x, y, right,station,train.getId(),terminus);
	}
	public Object[] getRow(int size) {
		Object []si=new Object[size];
		si[0]=train.getLibelle()+"("+train.getId()+")";
		si[1]=ligne.getStation(station).getLibelle();
		if(right)
		{
			si[2]=ligne.getStation(ligne.getNbStation()-1).getLibelle();
		}
		else
		{
			si[2]=ligne.getStation(0).getLibelle();
		}
		if(enMarche)
		{
			si[3]="démarré";
		}
		else
		{
			si[3]="Arrété";
		}
		if(terminus)
		{
			si[3]="terminus";
		}
		si[4]="("+x+','+y+")";
		return si;
	}
}
